﻿using K4os.Compression.LZ4;
using System.Net;
using ZeroMQCommon;

namespace ZeroMQReceiver
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, ZeroMQReceiver!");
            
            var assembler = new ImageAssembler(TimeSpan.FromSeconds(5));

            assembler.ImageCompleted += (frame, data) =>
            {
                byte[] raw = LZ4Pickler.Unpickle(data);
                Console.WriteLine($"[RECV] Completed ThreadId:{Thread.CurrentThread.ManagedThreadId}, Image: {frame.ImageId},ZSize :{data.Length / 1024 / 1024}MB, Size: {raw.Length / 1024 / 1024}MB,take{(frame.EndTime - frame.CreatedTime).TotalMilliseconds} ms");
                // TODO: send to detection pipeline
            };
            assembler.ImageTimeout += OnAssemblerImageTimeout;
            var receiver = new ZmqImageReceiver(GlobalConsts.ZMQ_IMAGE_SENDER_ADDRESS, assembler);
            receiver.StartReceiving();

            Task.Delay(Timeout.Infinite).Wait(); // Keep the application running
        }

        private static void OnAssemblerImageTimeout(string imageId)
        {
            Console.WriteLine($"[ImageTimeout] Completed Image: {imageId}");
        }
    }
}
